#!/bin/bash

#作者：夜微凉
#说明：此脚本用于免密登录服务器，目前仅支持9台服务器，如果不够，请自行添加。
#使用：
#cp link_servers.sh /tmp/link_servers.sh
#chmod +x /tmp/link_servers.sh
#echo -e "alias link='sh /tmp/link_servers.sh'" >> ~/.bashrc
#source ~/.bashrc

#添加颜色样式
Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m" && Red_background_yellow_font_prefix="\033[5;41;37m"

#服务器清单，在这里填写名称，IP，用户名和密码。
SERVER1_NAME="MyServer" && SERVER1_IP="xxxxxx" && SERVER1_USER="" && SERVER1_PASS=""
SERVER2_NAME="MyServer" && SERVER2_IP="xxxxxx" && SERVER2_USER="" && SERVER2_PASS=""
SERVER3_NAME="MyServer" && SERVER3_IP="xxxxxx" && SERVER3_USER="" && SERVER3_PASS=""
SERVER4_NAME="MyServer" && SERVER4_IP="xxxxxx" && SERVER4_USER="" && SERVER4_PASS=""
SERVER5_NAME="MyServer" && SERVER5_IP="xxxxxx" && SERVER5_USER="" && SERVER5_PASS=""
SERVER6_NAME="MyServer" && SERVER6_IP="xxxxxx" && SERVER6_USER="" && SERVER6_PASS=""
SERVER7_NAME="MyServer" && SERVER7_IP="xxxxxx" && SERVER7_USER="" && SERVER7_PASS=""
SERVER8_NAME="MyServer" && SERVER8_IP="xxxxxx" && SERVER8_USER="" && SERVER8_PASS=""
SERVER9_NAME="MyServer" && SERVER9_IP="xxxxxx" && SERVER9_USER="" && SERVER9_PASS=""

#判断系统版本
Get_Dist_Name()
{
    if grep -Eqi "CentOS" /etc/issue || grep -Eqi "CentOS" /etc/*-release; then
        DISTRO='CentOS'
        PM='yum'
    elif grep -Eqi "Red Hat Enterprise Linux Server" /etc/issue || grep -Eqi "Red Hat Enterprise Linux Server" /etc/*-release; then
        DISTRO='RHEL'
        PM='yum'
    elif grep -Eqi "Aliyun" /etc/issue || grep -Eqi "Aliyun" /etc/*-release; then
        DISTRO='Aliyun'
        PM='yum'
    elif grep -Eqi "Fedora" /etc/issue || grep -Eqi "Fedora" /etc/*-release; then
        DISTRO='Fedora'
        PM='yum'
    elif grep -Eqi "Debian" /etc/issue || grep -Eqi "Debian" /etc/*-release; then
        DISTRO='Debian'
        PM='apt'
    elif grep -Eqi "Ubuntu" /etc/issue || grep -Eqi "Ubuntu" /etc/*-release; then
        DISTRO='Ubuntu'
        PM='apt'
    elif grep -Eqi "Manjaro" /etc/issue || grep -Eqi "Manjaro" /etc/*-release; then
        DISTRO='Manjaro'
        PM='pacman'
    else
        DISTRO='unknow'
    fi
    #echo $DISTRO;
    
}

#自动安装sshpass（这个工具类似与expect，实现免交互功能）
install_sshpass(){
which sshpass >/dev/null 2>&1
if  [ $? = 1 ]; then
	#echo "未安装sshpass"
	if [ $DISTRO =  'Manjaro'  ]; then
        sudo pacman -S sshpass
    else
        sudo $PM -y install sshpass
    fi
fi

}

#回到菜单
Back_menu(){
if  [ ! $? = 0 ]; then
	#远程连接失败
	#清屏
	clear
	echo -e "${Red_background_yellow_font_prefix} 连接失败，请确认用户名和密码！${Font_color_suffix}"

else
	#远程连接成功
	#清屏
	clear
fi

#echo -e "\n\n\n"
#read -s -n1 -p "按任意键继续 ... "

#回到菜单
menu_status
}

# 显示 菜单状态
menu_status(){



#目前主机清单只能手动添加，附加功能还在开发中...
	echo -e "${Green_font_prefix}——————Linux Server远程连接助手——————${Font_color_suffix} 
	
——————主机清单——————
  ${Green_font_prefix}1.${Font_color_suffix} ${SERVER1_NAME}(${SERVER1_IP})
  ${Green_font_prefix}2.${Font_color_suffix} ${SERVER2_NAME}(${SERVER2_IP})
  ${Green_font_prefix}3.${Font_color_suffix} ${SERVER3_NAME}(${SERVER3_IP})
  ${Green_font_prefix}4.${Font_color_suffix} ${SERVER4_NAME}(${SERVER4_IP})
  ${Green_font_prefix}5.${Font_color_suffix} ${SERVER5_NAME}(${SERVER5_IP})
  ${Green_font_prefix}6.${Font_color_suffix} ${SERVER6_NAME}(${SERVER6_IP})
  ${Green_font_prefix}7.${Font_color_suffix} ${SERVER7_NAME}(${SERVER7_IP})
  ${Green_font_prefix}8.${Font_color_suffix} ${SERVER8_NAME}(${SERVER8_IP})
  ${Green_font_prefix}9.${Font_color_suffix} ${SERVER9_NAME}(${SERVER9_IP})
  
——————附加功能——————
  ${Green_font_prefix}10.${Font_color_suffix} 添加主机（开发中...）
  ${Green_font_prefix}11.${Font_color_suffix} 删除主机（开发中...）

————————————
  ${Green_font_prefix}q.${Font_color_suffix}  按q退出
 "
	echo && read -e -p "请输入数字：" num
	
case "$num" in
	1)
	#在这里手动添加主机
	#【-o StrictHostKeyChecking=no 表示远程连接时不提示是否输入yes/no】
	sshpass -p ${SERVER1_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER1_USER}@${SERVER1_IP}
	Back_menu
	;;
	2)
	sshpass -p ${SERVER2_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER2_USER}@${SERVER2_IP}
	Back_menu
	;;
	3)
	sshpass -p ${SERVER3_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER3_USER}@${SERVER3_IP}
	Back_menu
	;;
	4)
	sshpass -p ${SERVER4_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER4_USER}@${SERVER4_IP}
	Back_menu
	;;
	5)
	sshpass -p ${SERVER5_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER5_USER}@${SERVER5_IP}
	Back_menu
	;;
	6)
	sshpass -p ${SERVER6_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER6_USER}@${SERVER6_IP}
	Back_menu
	;;
	7)
	sshpass -p ${SERVER7_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER7_USER}@${SERVER7_IP}
	Back_menu
	;;
	8)
	sshpass -p ${SERVER8_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER8_USER}@${SERVER8_IP}
	Back_menu
	;;
	9)
	sshpass -p ${SERVER9_PASS} ssh  -o StrictHostKeyChecking=no ${SERVER9_USER}@${SERVER9_IP}
	Back_menu
	;;
	10)
	#添加主机
	Back_menu
	;;
	11)
	#删除主机
	Back_menu
	;;
	q)
	echo "拜拜~"
	sleep 1
	exit
	;;
	*)
	Back_menu
	;;
esac
}

#清屏
clear
#判断系统版本
Get_Dist_Name
#自动安装sshpass
install_sshpass
#显示主菜单
menu_status
